package Limit.BuffDoll;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.utils.SQLUtil;

public class BuffDoll_DollSkill_Table {
	
	private static BuffDoll_DollSkill_Table _instance;
	
	private final HashMap<Integer, BuffDoll_DollSkill_Load> _DollSkillInfo = new HashMap<Integer, BuffDoll_DollSkill_Load>();
	
	public static BuffDoll_DollSkill_Table getInstance() {
		if (_instance == null) {
			_instance = new BuffDoll_DollSkill_Table();
		}
		return _instance;
	}
	
	public int loadCount(int itemid)
	{
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		int i = 0;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM buffdoll_doll_skill WHERE Item_id = ?");
			pstm.setInt(1, itemid);
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				i++;
			}
		}
		
		catch (SQLException e) 
		{
			System.out.print(e.getMessage());
		}
		finally 
		{
			SQLUtil.close(rs, pstm, con);
		}	
		return i;
	}
	
	public void loadDollSkill_Info(int itemid) 
	{
		int count = loadCount(itemid);
		int[][] buffInfo = new int[count][7] ;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		int i;
		for (i = 0; i < count; i++)
			buffInfo[i][0] = 0;
		i = 0;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM buffdoll_doll_skill WHERE Item_id = ? Order By ID ASC");
			pstm.setInt(1, itemid);
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				buffInfo[i][0] = rs.getInt("Buff_ID");
				buffInfo[i][1] = rs.getInt("Buff_IMG");
				buffInfo[i][2] = rs.getInt("Time");
				buffInfo[i][3] = rs.getInt("Html_IMG");
				buffInfo[i][4] = rs.getInt("Place");
				buffInfo[i][5] = rs.getInt("Resident");
				buffInfo[i][6] = rs.getInt("ID");
				i++;
			}
		}
		catch (SQLException e) 
		{
			System.out.print(e.getMessage());
		}
		finally 
		{
			SQLUtil.close(rs, pstm, con);
		}
		
		BuffDoll_DollSkill_Load info = new BuffDoll_DollSkill_Load();
		Integer id = itemid;
		info.setItem_ID(id);
		info.setCount(count);
		info.setSkillInfo(buffInfo);

		_DollSkillInfo.put(id, info);
	}
	
	public void UpdateDollSkill_Info(int itemid, BuffDoll_DollSkill_Load load)
	{
		Connection con = null;	
		PreparedStatement pstm = null;
		
		int info[][] = load.getSkillInfo();
		for(int i = 0; i < info.length; i++){
			try {
				con = L1DatabaseFactory.getInstance().getConnection();
				pstm = con
						.prepareStatement("UPDATE buffdoll_doll_skill SET Place = ?, Resident = ? WHERE Item_ID=? AND ID =?");
				pstm.setInt(1, info[i][4]);
				pstm.setInt(2, info[i][5]);
				pstm.setInt(3, itemid);
				pstm.setInt(4, i + 1);
				pstm.execute();
			} 
			catch (Exception e) 
			{
				System.out.print(e.getMessage());
			} finally 
			{
				SQLUtil.close(pstm);
				SQLUtil.close(con);
			}
		}
	}
	
	public void StorageSkill(int itemid, int buffid, int buffimg, int time, int htmlimg, String name, BuffDoll_DollSkill_Load load)
	{	
		Connection con = null;	
		PreparedStatement pstm = null;
		int count = load.getCount();
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("INSERT INTO buffdoll_doll_skill SET Item_ID=?, Buff_ID=?, Buff_IMG=?, Time=?, Html_IMG =?, Buff_Name =?, ID =?");
			pstm.setInt(1, itemid);
			pstm.setInt(2, buffid);
			pstm.setInt(3, buffimg);
			pstm.setInt(4, time);
			pstm.setInt(5, htmlimg);
			pstm.setString(6, name);
			pstm.setInt(7,count+1);
			pstm.execute();
		} 
		catch (Exception e) 
		{
			System.out.print(e.getMessage());
		} finally 
		{
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public void delTemplate(int itemID) {
		_DollSkillInfo.remove(itemID);
	}
	
	public BuffDoll_DollSkill_Load getTemplate(int itemid) {
		if (_DollSkillInfo.containsKey(itemid)) {
			return _DollSkillInfo.get(itemid);
		}
		return null;
	}
}
